<template>
  <div>
    <div class="profile-box">
      <!-- 未登录头部 -->
      <div v-if="!user" class="not-login header">
        <img :src="userList.photo" alt="" @click="login">
        <span @click="login">登录 / 注册</span>
      </div>

      <!-- 已登录头部 -->
      <div v-else class="login-box header">
        <div class="user-info">
          <div class="left">
            <van-image
              class="img"
              round
              :src="userList.photo?userList.photo:'https://img01.yzcdn.cn/vant/cat.jpeg'"
            />
            <span class="user_name">{{ userList.name }}</span>
          </div>
          <van-button type="default" to="/user">编辑资料</van-button>
        </div>
        <div class="user-data">
          <div class="data-item">
            <span>{{ userList.art_count }}</span>
            <span>头条</span>
          </div>
          <div class="data-item">
            <span>{{ userList.follow_count }}</span>
            <span>关注</span>
          </div>
          <div class="data-item">
            <span>{{ userList.fans_count }}</span>
            <span>粉丝</span>
          </div>
          <div class="data-item">
            <span>{{ userList.like_count }}</span>
            <span>获赞</span>
          </div>
        </div>
      </div>

      <van-grid :border="false" :column-num="2" :gutter="1">
        <van-grid-item>
          <i class="toutiao toutiao-shoucang" />
          <span class="txt">收藏</span>
        </van-grid-item>
        <van-grid-item>
          <i class="toutiao toutiao-lishi" />
          <span class="txt">历史</span>
        </van-grid-item>
      </van-grid>

      <div class="cell-box">
        <van-cell title="消息通知" is-link />
        <van-cell title="小智同学" is-link />
      </div>

      <!-- 登录页面退出 -->
      <van-cell v-if="user" title="退出" class="loginout" @click="out" />
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { Dialog } from 'vant'
import { getUser } from '@/api/user.js'
export default {
  data() {
    return {
      userList: []
    }
  },
  computed: {
    ...mapState(['user'])
  },

  created() {
    this.getUser()
    // console.log(this.userList)
  },

  methods: {
    ...mapMutations(['removeUser']),
    out() {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出该账号'
      })
        .then(() => {
          // console.log(123)
          this.removeUser()
        })
        .catch(() => {
          // on cancel
        })
    },
    login() {
      this.$router.push('/login')
    },
    async getUser() {
      try {
        const res = await getUser()
        // console.log(res)
        this.userList = res
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.header{
 width: 750px;
  height: 401px;
  background-image: url(~@/assets/images/banner.png);
  background-size: 100% 100%;
}
.not-login{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  img{
    width: 132px;
    height: 132px;
  }
  span{
    font-size: 28px;
    color: #fff;
    margin-top: 15px;
  }
}
.login-box{
  overflow: hidden;
  .user-info{
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    margin-top: 116px;
    padding: 0 32px;
    width: 100%;
    height: 132px;
    // background-color: green;
  }
  .user-data{
    display: flex;
    width: 100%;
    height: 156px;
    // background-color: yellow;
    .data-item{
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #fff;
      span:first-child{
        font-size: 26px;
        margin-bottom: 8px;
      }
      span:last-child{
        font-size: 22px;
      }
    }
  }
}
.left{
  display: flex;
  align-items: center;
  .img{
    width: 132px;
    height: 132px;
    border: 1px solid #fff;
  }
  .user_name{
    margin-left: 22px;
    font-size: 30px;
    color: #fff;
  }
}

.van-button{
  width: 115px;
  height: 40px;
  background: #FFFFFF;
  border-radius: 40px;
  font-size: 16px;
  color: #666;
  padding: 0 5px;

}
.toutiao{
  font-size: 45px;
}
.toutiao-shoucang{
  color: #eb5253;
}
.toutiao-lishi{
  color: #ff9d1d;
}
.txt{
  margin-top: 16px;
  font-size: 28px;
}
.cell-box{
  margin: 10px 0;
}
.loginout{
  text-align: center;
}
</style>
